package com.sun.connection;

import com.sun.protocol.Protocol;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
/**
 * @author 烛龙神
 * @Description 连接层 连接redis数据库
 * @date 2021-04-07 21:22
 */
public class Connection {

    private Socket socket;
    private String host;
    private int port;
    private OutputStream outputStream;
    private InputStream inputStream;

    public Connection(String host, int port) {
        this.host = host;
        this.port = port;
    }

    public Connection sendCommand(Protocol.Command command,byte[] ...b)
    {
        //连接
        conenct();
        //发送
        Protocol.sendCommand(command,outputStream,b);
        return this;
    }
    public String getReply()
    {
        byte b[] = new byte[1024];
        try
        {
            socket.getInputStream().read(b);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new String(b);
    }
    public void conenct()
    {
        try {
            socket = new Socket(host,port);
            inputStream = socket.getInputStream();
            outputStream = socket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
